/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.repit.aglaja.client.contacts;

import ch.repit.rwt.client.Bento;
import ch.repit.rwt.client.BentoStatus;
import ch.repit.rwt.client.ObjectRef;
import ch.repit.rwt.client.pdf.StickersPrintingPage;
import ch.repit.rwt.client.persistence.CacheManager;
import ch.repit.rwt.client.security.Action;
import ch.repit.rwt.client.security.Authorizer;
import ch.repit.rwt.client.security.SecurityManager;
import ch.repit.rwt.client.ui.form.FormPage;
import ch.repit.rwt.client.ui.form.MultiSelectField;
import ch.repit.rwt.client.ui.form.TextField;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import java.util.List;


/**
 *
 * @author tc149752
 */
public class TagDetailsPage extends FormPage {

    ObjectRef tag2copy = null;

    public TagDetailsPage(ObjectRef objectRef)
    {
        super(TagDef.TYPE, objectRef, null);
    }

    
    public TagDetailsPage(ObjectRef shouldBeNull, ObjectRef tag2copy)
    {
        super(TagDef.TYPE, shouldBeNull, null);
        this.tag2copy = tag2copy;
    }


    @Override
    protected void init() {

        if (isCreate()) {
            if (tag2copy==null)
                setTitle("Création Tag");
            else
                setTitle("Duplication Tag");
        }
        else {
            setTitle(getObject().getDisplayValue(TagDef.ATTR_NAME));
        }
        super.setShowPath(true);

        // name
        TextField name = new TextField(TagDef.ATTR_NAME);
        this.addSingleFieldRow("Nom du tag", null, name);

        // Description
        TextField desc = new TextField(TagDef.ATTR_DESCRIPTION, null, 3);
        this.addSingleFieldRow("Description", null, desc);

        // liste des adresses
        MultiSelectField addresses = new MultiSelectField(TagDef.ATTR_ADDRESSES, "Adresses", "disponibles", "sélectionnées");
        addresses.setAvailableValues(AddressUtil.getAddressesSelectMap());
        this.addSingleFieldRow("Addresses", "Il est plus facile de passer par la liste des addresses pour éditer le contenu", addresses);

        // tous les champs editables seul. si actif...
        if (!isCreate() && getObject().getStatus() != BentoStatus.ACTIVE) {
            name.setReadOnly(true);
            desc.setReadOnly(true);
            addresses.setReadOnly(true);
        }

        super.init();

        // after super.init, so that content of list is not lost
        if (tag2copy != null) {
            Bento ttc = CacheManager.get().getCachedObject(tag2copy);
            if (ttc != null) {
                name.setText(ttc.getDisplayValue(TagDef.ATTR_NAME) + " (copy)");
                desc.setText(ttc.getDisplayValue(TagDef.ATTR_DESCRIPTION));
                addresses.setDefaultValue(ttc.get(TagDef.ATTR_ADDRESSES).getStringList());
            }
        }
    }

    @Override
    protected void fillToolbarWidgets(List<Widget> leftWidgets,
                                      List<Widget> middleWidgets,
                                      List<Widget> rightWidgets) {
        super.fillToolbarWidgets(leftWidgets, middleWidgets, rightWidgets);

        if (getObject().getStatus() == BentoStatus.ACTIVE) {
            Button archiveButton = new Button("Archiver");
            archiveButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent arg0) {
                    getObject().setStatus(BentoStatus.ARCHIVE);
                    doSave(getObject());
                }
            });
            leftWidgets.add(archiveButton);
        }
        
        if (getObject().getStatus() == BentoStatus.ARCHIVE) {
            Authorizer auth = SecurityManager.get().getAuthorizer();
            if (auth.isAllowed(Action.UPDATE, super.getObject())) {
                Button reacButton = new Button("Réactiver");
                reacButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent arg0) {
                        doChangeState(getObject(), BentoStatus.ACTIVE, "réactivation");
                    }
                });
                leftWidgets.add(reacButton);
            }
        }

        if (getObject().getStatus() == BentoStatus.ARCHIVE || getObject().getStatus() == BentoStatus.ACTIVE) {
            Button stickers = new Button("Etiquettes");
            stickers.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent arg0) {
                    getPageNav().displayInPopup(new StickersPrintingPage(null,getObject().getRef()), true);
                }
            });
            rightWidgets.add(stickers);
        }

        if (!isCreate()) {
            Authorizer auth = SecurityManager.get().getAuthorizer();
            if (auth.isAllowed(Action.CREATE, getObject())) {
                Button reacButton = new Button("Dupliquer");
                reacButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent arg0) {
                        getPageNav().back();
                        getPageNav().displayPage(new TagDetailsPage(null, getObject().getRef()));
                    }
                });
                rightWidgets.add(reacButton);
            }
        }
    }

}
